package com.upf1sh.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upf1sh.bean.SensorData;
import com.upf1sh.mapper.SensorMapper;
import com.upf1sh.service.SensorDataService;
import com.upf1sh.vo.PageVO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorMapper, SensorData> implements SensorDataService {

    @Autowired
    private SensorMapper sensorMapper;

    @Override
    /** 分页查询传感器数据 */
    public PageVO selectSensorData(Integer pageNumber, Integer pageSize) {
        PageVO pageVO = new PageVO();
        int selectPageSize = Math.max(10, Math.min(pageSize, 20));
        int selectPageNumber = Math.max(pageNumber, 1);

        long total = selectSensorDataCount();
        pageVO.setTotal(total);

        List<SensorData> sensorDataList = selectSensorDataList(selectPageSize,selectPageSize * (selectPageNumber -1));

        pageVO.setRows(sensorDataList);
        pageVO.setPageNumber(selectPageNumber);
        pageVO.setPageSize(selectPageSize);

        return pageVO;

    }

    @Override
    public int add(SensorData sensorData) {
        return sensorMapper.insert(sensorData);
    }


    /** 查询总条数 */
    public long selectSensorDataCount() {
        return sensorMapper.selectCount(null);
    }

    /** 依据页数查询集合*/
    public List<SensorData> selectSensorDataList(Integer pageSize, Integer pageStart) {
        return sensorMapper.selectSensorDataList(pageSize,pageStart);
    }


    public boolean get(){
        boolean flag = false;
        List<SensorData> sensorList = new ArrayList<>();
        //1.请求接口
        HttpClient client = HttpClients.createDefault();
        String url = "http://106.12.172.133:9060/api/SensingData/Post_ReadSensingData";

        // 创建HttpPost实例
        HttpPost post = new HttpPost(url);

        // 创建JSON格式的请求体
        String json = "{\"user_name\":\"sa\", \"pass_word\":\"ias1957\"}";
        HttpEntity responseEntity = null;
        String result = null;

        try {
            // 设置请求头Content-Type为application/json
            post.setHeader("Content-Type", "application/json");
            // 设置请求体
            StringEntity entity = new StringEntity(json);
            post.setEntity(entity);

            // 发送POST请求
            HttpResponse response = client.execute(post);

            // 获取响应实体
            responseEntity = response.getEntity();

            // 打印响应状态码
//            System.out.println("Response Code: " + response.getStatusLine().getStatusCode());

            // 打印响应内容
            result = EntityUtils.toString(responseEntity);
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //2.封装集合
        if (responseEntity != null) {

            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONArray jsonArray = jsonObject.getJSONArray("Values");
            // 遍历JSON数组并添加到ArrayList中
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject sensorJsonObject = jsonArray.getJSONObject(i);
                SensorData sensorData = new SensorData();

                sensorData.setValuesFood(sensorJsonObject.getFloat("ValuesFood"));
                sensorData.setAwareID(sensorJsonObject.getString("AwareID"));
                sensorData.setValuesStep(sensorJsonObject.getInteger("ValuesStep"));
                sensorData.setTimestamp(sensorJsonObject.getTimestamp("Timestamp").toLocalDateTime());
                sensorData.setValuesTemperature(sensorJsonObject.getFloat("ValuesTemperature"));
                sensorData.setValuesWater(sensorJsonObject.getFloat("ValuesWater"));
                sensorList.add(sensorData);
            }
            for (SensorData sensorData : sensorList) {
                System.out.println(sensorData.getValuesFood());
                sensorMapper.insert(sensorData);
                flag = true;
            }
        } else {
            flag = false;
        }
        return flag;
    }


}
